//
//  ST_LiveBeautyRulesController.swift
//  btvideo
//
//  Created by Jason on 2021/12/22.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import SDWebImage
import SwiftyUserDefaults
import RxSwift

class ST_LiveBeautyRulesController: UIViewController {

    let isVideo:Bool
    
    init(isVideo:Bool){
        
        self.isVideo = isVideo
        
        super.init(nibName: nil, bundle: nil)
        self.overCurrentContext = true
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        view.addSubview(rules_image_view)
        rules_image_view.snp.makeConstraints { make in
            make.width.equalTo(300)
            make.height.equalTo(392)
            make.center.equalToSuperview()
        }
        
        self.show_indeterminate_h_u_d()
        
        rules_image_view.sd_setImage(with: background_url) {[weak self] _, _, _, _ in
            
            self?.hidden_indeterminate_h_u_d()
            
            self?.rules_image_view.alpha = 1
            
        }
        
    }
    
    private lazy var background_url:URL? = {
        if isVideo{
            
            return SCComponets.rich_text_storage_component.json["live_match_rules"]["video"].url
        }
        return SCComponets.rich_text_storage_component.json["live_match_rules"]["voice"].url
    }()
    
    fileprivate lazy var rules_image_view:UIImageView = {
        let v = UIImageView()
        v.isUserInteractionEnabled = true
        v.addSubview(self.rules_confirm_btn)
        v.alpha = 0
        rules_confirm_btn.snp.makeConstraints { make in
            make.width.equalTo(232)
            make.height.equalTo(44)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-20)
        }
        return v
    }()
    
    fileprivate lazy var rules_confirm_btn:UIButton = {
        let btn = UIButton.init(type: UIButton.ButtonType.custom)
        btn.setTitleColor(.white, for: .normal)
        btn.backgroundColor =  UIColor(hex: 0xFEC207)
        btn.setTitle("开始速配", for: .normal)
        btn.cornerRadius = 22
        //btn.semanticContentAttribute = .forceRightToLeft
        btn.addTarget(self, action: #selector(rules_confirm_handle), for: UIControl.Event.touchUpInside)
        return btn
    }()
    
    var nextStep = PublishSubject<Void>()
    
    @objc fileprivate func rules_confirm_handle(){
        
        self.rules_confirm_btn.isUserInteractionEnabled = false
        
        
        
        if isVideo{
            Defaults[.videoRulesToasted] = true
        }else{
            Defaults[.voiceRulesToasted] = true
        }
        
        self.goback_previous_controller(animated: false) {
            self.nextStep.onNext(())
        }
        
        
    }


}
